package com.fsnbigdata.web;

import com.fsnbigdata.domain.ZhHelp;
import com.fsnbigdata.enums.ResultEnum;
import com.fsnbigdata.exception.ParamException;
import com.fsnbigdata.service.api.ZhHelpService;
import com.fsnbigdata.util.ReflectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType;
import org.springframework.roo.addon.web.mvc.controller.annotations.RooController;
import org.springframework.roo.addon.web.mvc.controller.annotations.responses.json.RooJSON;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: chen quan zhou
 * @Date: Created in 16:49 2018/9/29
 * @Modified By:
 */

@RooController(entity = ZhHelp.class, pathPrefix = "/api", type = ControllerType.COLLECTION)
@RooJSON
@RestController
@RequestMapping(value = "/api/zhhelp", name = "ZhHelpController", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "ZhHelp", description = "帮助")
public class ZhHelpController {
    


    private ZhHelpService zhHelpService;

    @Autowired
    public ZhHelpController( ZhHelpService zhHelpService) {
        this.zhHelpService = zhHelpService;
    }


    public ZhHelpService getZhkopuService() {
        return zhHelpService;
    }


    public void setAnimalConservationService(ZhHelpService zhHelpService) {
        this.zhHelpService = zhHelpService;
    }

    /**
     * 查询id为1的记录
     *
     * @return
     */
    @GetMapping(value = "findById")
    @PreAuthorize("hasRole('DB_ZHHELP')")
    public ZhHelp findById(Authentication authentication) {
        List<String> authorities = authentication.getAuthorities().stream().map(it -> it.getAuthority()).collect(Collectors.toList());
        String creator=null;
        if(!authorities.contains("ROLE_USER_ADMIN")){
            creator = authentication.getName();
        }
        return getZhkopuService().findById(Long.valueOf(1),creator);
    }
    
    /**
     * 修改记录
     *
     * @param zhHelp
     * @param result
     * @return
     */
    @PutMapping(value = "updatezhhelp")
    @PreAuthorize("hasRole('DB_ZHHELP')")
    @ApiOperation(value = "",notes = "update")
    public ResponseEntity<?> update(@Valid @RequestBody ZhHelp zhHelp, BindingResult result,Principal principal) throws Exception {
        if (result.hasErrors()) {
            throw new ParamException(ResultEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }

        String content = getZhkopuService().findOne(zhHelp.getId()).getContent();
        zhHelp.setAdd_time(new Date());
        zhHelp.setCreator(principal.getName());

        ZhHelp newZhHelp =  getZhkopuService().update(zhHelp);
        Class<?> clazz = ZhHelpController.class;
        Method method = clazz.getMethod("update", ReflectUtil.getPeremeterClass(clazz, "update"));
        ReflectUtil.setAnnotationOn(method,"更新帮助，内容："+content);
        return ResponseEntity.ok(newZhHelp);
    }
    
}
